import { defineStore } from 'pinia'
import { ref } from 'vue'

import { userGetInfoService } from '@/api/user'

export const useUserStore = defineStore(
  'user',
  () => {
    const token = ref('')

    const setToken = newToken => {
      token.value = newToken
    }

    // 清除Token信息
    const removeToken = () => {
      token.value = ''
    }

    const user = ref({})

    //获取用户信息
    const getuser = async () => {
      const res = await userGetInfoService()
      // console.log(res)
      user.value = res.data
    }

    // 清除用户信息
    const removeuser = obj => {
      user.value = obj
    }

    return {
      token,
      setToken,
      removeToken,
      user,
      getuser,
      removeuser
    }
  },
  {
    persist: true
  }
)
